﻿using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace SSR
{

    class Tool
    {
        public static string httpreq(string url, int Timeout)
        {
            ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            //request.Timeout = Timeout;


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        public static List<float> strangeRanmdom(int times)
        {
            List<float> returnc = new List<float>();
            var a = new Random().Next(1, 10000);
            var b = new Random().Next(10, 100);
            for (int i = 0; i < times; i++)
            {
                a = a * b;
                var temp1 = new Random(a).Next(1, 10000);
                returnc.Add((float)temp1 / 10000);
                b++;
            }
            return returnc;
        }

    }

    class ssrcards
    {
        public ssrcards()
        {
            cards = new List<Card>();
        }
        public List<Card> cards { get; set; }
    }
    public class Card
    {
        //private string myname;
        public string name
        {
            get;

            set;

        }
        public int level { get; set; }
        public int p { get; set; }

        public void set1(string name2, int level2, int p2)
        {
            name = name2;
            level = level2;
            p = p2;

        }
        //static public List<Card> getCardfromPool(string poolJsonPath, int times)
        //{
        //    List<Card> returnc = null;
        //    var list3 = new List<Card>();
        //    var list4 = new List<Card>();
        //    var list5 = new List<Card>();
        //    var list6 = new List<Card>();

        //    var jsonText = new StreamReader(poolJsonPath).ReadToEnd();
        //    ssrcards ssr = JsonMapper.ToObject<ssrcards>(jsonText);
        //    foreach (var item in ssr.cards)
        //    {
        //        if (item.level == 3)
        //        {
        //            list3.Add(item);
        //        }
        //        else if (item.level == 4)
        //        {
        //            list4.Add(item);
        //        }
        //        else if (item.level == 5)
        //        {
        //            list5.Add(item);
        //        }
        //        else if (item.level == 6)
        //        {
        //            list6.Add(item);
        //        }
        //    }//分配 可展开
        //    Debug.Print(list3.Count.ToString());
        //    var r = new Random();
        //    //var plevel = r.NextDouble();
        //    //获取int个随机数
        //    var getstring = Tool.httpreq(String.Format("https://www.random.org/integers/?num={0}&min=1&max=100&col=1&base=10&format=plain&rnd=new", times.ToString()), 4000);
        //    string[] str2 = { "1" };
        //    try
        //    {
        //        getstring.Replace("\n", "^");
        //        char a = '^';
        //        str2 = getstring.Split(a);
        //    }
        //    catch
        //    {

        //    }

        //    for (int i = 0; i < times; i++)
        //    {
        //        float plevel;
        //        try
        //        {
        //            plevel = Int32.Parse(str2[i]) / 1000;
        //        }
        //        catch (Exception)
        //        {
        //            plevel = 45 / 100;
        //            //throw;
        //        }

        //        {

        //            if (plevel <= 0.4)
        //            {
        //                returnc.Add(list3[r.Next(1, list3.Count) - 1]);
        //            }
        //            else if (plevel <= 0.7)
        //            {
        //                returnc.Add(list4[r.Next(1, list4.Count) - 1]);
        //            }
        //            else if (plevel <= 0.9)
        //            {
        //                returnc.Add(list5[r.Next(1, list5.Count) - 1]);
        //            }
        //            else
        //            {
        //                returnc.Add(list6[r.Next(1, list6.Count) - 1]);
        //            }
        //        }//抽卡分配
        //    }

        //    return returnc;
        //}

    }
    /// <summary>
    /// 其实目前只有一个卡池
    /// </summary>
    public class CardList
    {
        bool inited;
        private List<Card> list3;
        private List<Card> list4;
        private List<Card> list5;
        private List<Card> list6;
        private List<Card> Cards { get; set; }

        /// <summary>
        /// 实例化一个卡池系统
        /// </summary>
        /// <param name="jsonText">卡池Json文本</param>
        public CardList(string jsonText)
        {
            if (string.IsNullOrEmpty(jsonText))
            {
                //throw new ArgumentException("message", nameof(jsonText));
            }
            else
            {
                var j = JsonMapper.ToObject(jsonText);
                var j2 = j["cards"];
                Cards = JsonMapper.ToObject<List<Card>>(j2.ToJson());
                inited = false;
            }
        }
        public void debugList()
        {
            string str = "";
            {
                foreach (var item in list3)
                {
                    str += item.name + "\n";
                }
                foreach (var item in list4)
                {
                    str += item.name + "\n";
                }
                foreach (var item in list5)
                {
                    str += item.name + "\n";
                }
                foreach (var item in list6)
                {
                    str += item.name + "\n";
                }
            }//遍历所有list，跑到str里面去！
            Console.WriteLine(str);
        }//测试用 输出所有卡
        /// <summary>
        /// 初始化卡池
        /// </summary>
        public bool initPool()
        {
            try
            {
                list3 = new List<Card>();
                list4 = new List<Card>();
                list5 = new List<Card>();
                list6 = new List<Card>();
                foreach (Card item in Cards)
                {
                    if (item.level == 3)
                    {
                        list3.Add(item);
                    }
                    else if (item.level == 4)
                    {
                        list4.Add(item);
                    }
                    else if (item.level == 5)
                    {
                        list5.Add(item);
                    }
                    else if (item.level == 6)
                    {
                        list6.Add(item);
                    }
                }
                inited = true;
                return true;

            }
            catch
            {
                inited = false;
                return false;
            }

        }

        /// <summary>
        /// 得到相应次数的卡List
        /// </summary>
        /// <param name="times">次数</param>
        /// <returns></returns>
        public List<Card> getCards(int times)
        {
            var returnc = new List<Card>();

            var plevel = Tool.strangeRanmdom(times);
            var d = new Random().Next(203802,29272922);
            for (int i = 0; i < times; i++)
            {
                d *= 3;
                var r = new Random(d);
                if (plevel[i] <= 0.4)
                {
                    returnc.Add(list3[r.Next(1, list3.Count + 1) - 1]);
                }
                else if (plevel[i] <= 0.7)
                {
                    returnc.Add(list4[r.Next(1, list4.Count + 1) - 1]);
                }
                else if (plevel[i] <= 0.9)
                {
                    returnc.Add(list5[r.Next(1, list5.Count+1) - 1]);
                }
                else
                {
                    returnc.Add(list6[r.Next(1, list6.Count + 1) - 1]);
                }

            }
            return returnc;
        }
        /// <summary>
        /// 获取概率详细文本，待2.0修改
        /// </summary>
        /// <returns></returns>
        public string getPossibilityText()
        {
            var returnc = string.Empty;
            if (inited == true)
            {
                //returnc += "六星综合概率10%\n" + "其中共有" + list6.Count + "个物品,平均概率" + (float)10 / list6.Count + "%\n"
                //    + "分别为\n" + ListToString(Common.ccardList.list6)
                //    + "\n五星综合概率20%\n" + "其中共有" + list5.Count + "个物品,平均概率" + (float)20 / list5.Count + "%\n"
                //    + "分别为\n" + ListToString(Common.ccardList.list5)
                //    + "\n四星综合概率30%\n" + "其中共有" + list4.Count + "个物品,平均概率" + (float)30 / list4.Count + "%\n"
                //    + "分别为\n" + ListToString(Common.ccardList.list4)
                //    + "\n三星综合概率40%\n" + "其中共有" + list3.Count + "个物品,平均概率" + (float)40 / list3.Count + "%\n"
                //    + "分别为\n" + ListToString(Common.ccardList.list3);



            }
            else
            {
                throw new Exception("初始化不成功");
            }
            return returnc;
        }
        public bool isInited()
        {
            return inited;
        }
        private string ListToString(List<Card> list)
        {
            var str = "";
            foreach (Card item in list)
            {
                str += item.name + "\n";
            }
            char char1 = '\n';
            str = str.TrimEnd(char1);
            return str;
        }

    }
}
